import 'package:lib_common/lib_common.dart';

class SlipWidget extends StatefulWidget {
  final Widget child;

  final Function? onSlipLeft;
  final Function? onSlipUp;
  final Function? onSlipRight;
  final Function? onSlipDown;

  const SlipWidget(
      {super.key,
      required this.child,
      this.onSlipLeft,
      this.onSlipUp,
      this.onSlipRight,
      this.onSlipDown});

  @override
  State<SlipWidget> createState() => _SlipWidgetState();
}

class _SlipWidgetState extends State<SlipWidget> {
  double startX = 0;

  double startY = 0;

  double moveX = 0;

  double moveY = 0;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onPanDown: (value) {
          startX = value.globalPosition.dx;
          startY = value.globalPosition.dy;
        },
        onPanUpdate: (value) {
          moveX = value.globalPosition.dx - startX;
          moveY = value.globalPosition.dy - startY;
        },
        onPanEnd: (value) {
          if (moveX.abs() > moveY.abs()) {
            var scrollWidth = ScreenUtils.designHeight.x / 8;
            // LogUtils.v("scrollWidth:$scrollWidth");
            if (moveX > scrollWidth) {
              widget.onSlipRight?.call();
            }
            if (moveX < -scrollWidth) {
              widget.onSlipLeft?.call();
            }
          } else {
            var scrollHeight = ScreenUtils.designHeight.y / 8;
            // LogUtils.v("scrollHeight:$scrollHeight");
            if (moveY > scrollHeight) {
              widget.onSlipDown?.call();
            }
            if (moveY < -scrollHeight) {
              widget.onSlipUp?.call();
            }
          }
        },
        child: widget.child);
  }
}
